package itsm.isperp.module.utils;

import itsm.isperp.module.dto.configuration.ConfCiPropertyDTO;
import itsm.isperp.module.entity.configuration.ConfCategoryProperty;
import itsm.isperp.module.entity.configuration.ConfCiProperty;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2014-9-25
 * @since 1.0
 * 
 */
public class CiProprtyUtils {

	public static final String getPropertyDescription(
			Map<String, ConfCiProperty> newProperties,
			Map<String, ConfCiProperty> oldProperties,
			Map<String, ConfCategoryProperty> cateMap) {

		StringBuilder sb = new StringBuilder();

		for (String pKey : cateMap.keySet()) {
			ConfCiProperty newP = newProperties.get(pKey);
			ConfCiProperty oldP = oldProperties.get(pKey);

			if (newP == null) {
				if (oldP != null && StringUtils.isNotEmpty(oldP.getValue())) {
					// oldP is not null
					sb.append("【" + cateMap.get(pKey).getName() + "】："
							+ oldP.getValue() + "->;");
				}

			} else {

				if (oldP == null) {
					if (StringUtils.isNotEmpty(newP.getValue())) {
						sb.append("【" + cateMap.get(pKey).getName() + "】："
								+ newP.getValue() + ";");
					}
				} else {

					if (StringUtils.isEmpty(oldP.getValue())
							|| !newP.getValue().equals(oldP.getValue())) {
						sb.append("【" + cateMap.get(pKey).getName() + "】："
								+ oldP.getValue() + "->" + newP.getValue()
								+ ";");

					}

				}
			}

		}

		return sb.toString();
	}

	public static final String getPropertyDescription(
			Collection<ConfCiProperty> properties,
			Map<String, ConfCategoryProperty> cateMap, String split) {

		StringBuilder sb = new StringBuilder();
		for (ConfCiProperty p : properties) {
			sb.append(cateMap.get(p.getConfCategoryPropertyId()).getName()
					+ split + p.getValue() + ";");
		}
		return sb.toString();
	}

	public static final String getPropertyDescription(
			List<ConfCiPropertyDTO> properties) {
		StringBuilder sb = new StringBuilder();
		for (ConfCiPropertyDTO p : properties) {
			sb.append(p.getPropertyName() + ":" + p.getPropertyValue() + ";");
		}
		return sb.toString();

	}

}
